﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "IElementShapeHandle.h"
#include "GbmpCalculatorBase.h"
#include "NewLevel.h"
#include "IGenericElement.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

namespace gcmp
{
    class NewLevelShapeHandlerGRepCalculator: public gcmp::GbmpCalculatorBase
    {
        DECLARE_CALCULATOR(NewLevelShapeHandlerGRepCalculator, IElementShapeHandle)

    public:
        NewLevelShapeHandlerGRepCalculator(IDocument* pDoc, const RegenDataId& outputDataId) : GbmpCalculatorBase(pDoc, outputDataId){}
        virtual void ReportInputDataIds(std::vector<RegenDataId> & dataIds) const override;
        virtual void Execute() override;
    };

    void NewLevelShapeHandlerGRepCalculator::ReportInputDataIds(std::vector<RegenDataId> & dataIds) const
    {
        IElementShapeHandle* pAuxiliaryPoint = GetElement<IElementShapeHandle>();
        DBG_WARN_AND_RETURN_VOID_UNLESS(pAuxiliaryPoint, L"无效参数",L"GDMPLab",L"2024-03-30");
        IGenericElement* pGenericElement = dynamic_cast<IGenericElement*>(GetDocument()->GetElement(pAuxiliaryPoint->GetMasterId()));
        DBG_WARN_AND_RETURN_VOID_UNLESS(pGenericElement, L"Master Element不存在",L"GDMPLab",L"2024-03-30");
        NewLevel* pLevel = quick_cast<NewLevel>(pGenericElement->GetExternalObject());
        DBG_WARN_AND_RETURN_VOID_UNLESS(pGenericElement, L"Master Element不存在",L"GDMPLab",L"2024-03-30");
        dataIds.push_back(pAuxiliaryPoint->GetMasterIdRdIdForBehavior());
        dataIds.push_back(pLevel->GetReferencePlaneRdId());
    }

    void NewLevelShapeHandlerGRepCalculator::Execute()
    {
        IElementShapeHandle* pAuxiliaryPoint = GetElement<IElementShapeHandle>();
        
        // 其实只需要触发一次IElementShapeHandle的数据修改，用来刷新Handle的显示
        // GetElementConstraintBehavior()实际调用的是GetElementShapeHandleBehaviorFW__(),该函数实际已经触发数据修改
        IElementShapeHandleBehavior* handleBehavior = pAuxiliaryPoint->GetElementShapeHandleBehavior();

        // UpdatePosition() 可以什么也不做，就是个形式调用，甚至可以不调用这个函数
        handleBehavior->UpdatePosition();
    }
}

IMPLEMENT_CALCULATOR_CREATOR(gcmp, NewLevelShapeHandlerGRepCalculator);



